home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / aptdaemon / debconf.py < prev    next >
Text File  |  2009-10-14  |  7KB  |  196 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Integration of debconf on the client side
  5.  
  6. Provides the DebconfProxy class which allows to run the debconf frontend
  7. as normal user by connecting to the root running debconf through the
  8. socket of the passthrough frontend.
  9. """
  10. # Copyright (C) 2009 Sebastian Heinlein <devel@glatzor.de>
  11. # Copyright (C) 2009 Michael Vogt <michael.vogt@ubuntu.com>
  12. #
  13. # This program is free software; you can redistribute it and/or modify
  14. # it under the terms of the GNU General Public License as published by
  15. # the Free Software Foundation; either version 2 of the License, or
  16. # any later version.
  17. #
  18. # This program is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. # GNU General Public License for more details.
  22. #
  23. # You should have received a copy of the GNU General Public License along
  24. # with this program; if not, write to the Free Software Foundation, Inc.,
  25. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  26.  
  27. import copy
  28. import glib
  29. import logging
  30. import os
  31. import os.path
  32. import shutil
  33. import socket
  34. import subprocess
  35. import tempfile
  36.  
  37. import gobject
  38.  
  39. log = logging.getLogger("AptClient.DebconfProxy")
  40.  
  41. class DebconfProxy(object):
  42.  
  43.     """The DebconfProxy class allows to run the debconf frontend
  44.     as normal user by connecting to the root debconf through the socket of the
  45.     passthrough frontend.
  46.     """
  47.  
  48.     def __init__(self, frontend="gnome", socket_path=None):
  49.         """Initialize a new DebconfProxy instance.
  50.  
  51.         Keyword arguments:
  52.         frontend -- the to be used debconf frontend (defaults to gnome)
  53.         socket_path -- the path to the socket of the passthrough frontend.
  54.             Will be created if not specified
  55.         """
  56.         self.socket_path = socket_path
  57.         self.temp_dir = None
  58.         if socket_path is None:
  59.             self.temp_dir = tempfile.mkdtemp(prefix="aptdaemon-")
  60.             self.socket_path = os.path.join(self.temp_dir, "debconf.socket")
  61.         log.debug("debconf socket: %s" % self.socket_path)
  62.         self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  63.         self.socket.bind(self.socket_path)
  64.         self.frontend = frontend
  65.         self._listener_id = None
  66.         self._active_conn = None
  67.         self._watch_ids = []
  68.  
  69.     def _get_debconf_env(self):
  70.         """Returns a dictonary of the environment variables required by
  71.         the debconf frontend.
  72.         """
  73.         env = copy.copy(os.environ)
  74.         env["DEBCONF_DB_REPLACE"] = "configdb"
  75.         env["DEBCONF_DB_OVERRIDE"] = "Pipe{infd:none outfd:none}"
  76.         env["DEBIAN_FRONTEND"] = self.frontend
  77.         if log.level == logging.DEBUG:
  78.             env["DEBCONF_DEBUG"] = "."
  79.         return env
  80.  
  81.     def start(self):
  82.         """Start listening on the socket."""
  83.         logging.debug("debconf.start()")
  84.         self.socket.listen(1)
  85.         self._listener_id = gobject.io_add_watch(self.socket, gobject.IO_IN,
  86.                                                  self._accept_connection)
  87.  
  88.     def stop(self):
  89.         """Stop listening on the socket."""
  90.         logging.debug("debconf.stop()")
  91.         self.socket.close()
  92.         # ensure outstanding gio messages are processed
  93.         context = glib.main_context_default()
  94.         while context.pending():
  95.             context.iteration()
  96.         gobject.source_remove(self._listener_id)
  97.         self._listener_id = None
  98.  
  99.     def _accept_connection(self, source, condition):
  100.         """Callback for new connections of the passthrough frontend."""
  101.         log.debug("New passthrough connection")
  102.         # ensure outstanding gio messages are processed (to ensure
  103.         # that _hangup was run on the previous connection, see LP: #432607)
  104.         context = glib.main_context_default()
  105.         while context.pending():
  106.             context.iteration()
  107.         if self._active_conn:
  108.             raise IOError, "Multiple debconf connections not supported"
  109.         conn, addr = source.accept()
  110.         self._active_conn = conn
  111.         self.helper = subprocess.Popen(["debconf-communicate"],
  112.                                        stdin=subprocess.PIPE,
  113.                                        stdout=subprocess.PIPE,
  114.                                        env=self._get_debconf_env())
  115.         w = gobject.io_add_watch(conn, gobject.IO_IN|gobject.IO_HUP|gobject.IO_ERR,
  116.                              self._copy_conn, self.helper.stdin)
  117.         self._watch_ids.append(w)
  118.         w= gobject.io_add_watch(self.helper.stdout, gobject.IO_IN,
  119.                              self._copy_stdout, conn)
  120.         self._watch_ids.append(w)
  121.         w = gobject.io_add_watch(self.helper.stdout, gobject.IO_HUP|gobject.IO_ERR,
  122.                              self._hangup)
  123.         self._watch_ids.append(w)
  124.         return True
  125.  
  126.     def _hangup(self, source, condition):
  127.         """Callback when the debconf-communicate program exists 
  128.            This happend when e.g. the user cancels the frontend
  129.         """
  130.         logging.debug("hangup on %s" % source)
  131.         if self._watch_ids:
  132.             for w in self._watch_ids:
  133.                 gobject.source_remove(w)
  134.             self._watch_ids = []
  135.         if self._active_conn:
  136.             self._active_conn.close()
  137.             self._active_conn = None
  138.         if self.helper:
  139.             self.helper.stdin.close()
  140.             self.helper.stdout.close()
  141.             self.helper.wait()
  142.             self.helper = None
  143.         return False
  144.  
  145.     def _copy_stdout(self, source, condition, conn):
  146.         """Callback to copy data from the stdout of debconf-communicate to
  147.         the passthrough frontend."""
  148.         logging.debug("_copy_stdout")
  149.         try:
  150.             debconf_data = source.readline()
  151.             if debconf_data:
  152.                 log.debug("From debconf: %s", debconf_data)
  153.                 conn.send(debconf_data)
  154.                 return True
  155.         except:
  156.             pass
  157.         # error, stop listening
  158.         return self._hangup(source, condition)
  159.  
  160.     def _copy_conn(self, source, condition, stdin):
  161.         """Callback to copy data from the passthrough frontend to stdin of
  162.         debconf-communicate."""
  163.         logging.debug("_copy_conn")
  164.         try:
  165.             socket_data = source.recv(1024)
  166.             if socket_data:
  167.                 log.debug("From socket: %s", socket_data)
  168.                 stdin.write(socket_data)
  169.                 return True
  170.         except:
  171.             pass
  172.         # errror, stop listening
  173.         return self._hangup(source, condition)
  174.  
  175. def _test():
  176.     """Run the DebconfProxy from the command line for testing purposes.
  177.  
  178.     You have to execute the following commands before in a separate terminal:
  179.     $ echo "fset debconf/frontend seen false" | debconf-communicate
  180.     $ export DEBCONF_PIPE=/tmp/debconf.socket
  181.     $ dpkg-reconfigure debconf -f passthrough
  182.     """
  183.     logging.basicConfig(level=logging.DEBUG)
  184.     socket_path="/tmp/debconf.socket"
  185.     if os.path.exists(socket_path):
  186.         os.remove(socket_path)
  187.     proxy = DebconfProxy("gnome", socket_path)
  188.     proxy.start()
  189.     loop = gobject.MainLoop()
  190.     loop.run()
  191.  
  192. if __name__ == "__main__":
  193.     _test()
  194.  
  195. #vim:ts=4:sw=4:et
  196.